<?php


namespace app\api\library\login\updateUser;


use app\api\model\MallUser;

class V
{
    public static function run($param)
    {
        //查询有无openid
        _checkEmptyParam($param[ 'openId' ], 'openId不能为空');
        _checkEmptyParam($param[ 'mobile' ], '手机号不能为空');
        // 获取用户ID
        $user = MallUser::where('open_id', $param[ 'openId' ])->find();
        $data = [
            'open_id'     => $param[ 'openId' ],
            'wx_mobile'   => $param[ 'mobile' ],
            'nick_name'   => $param[ 'nickName' ],
            'avatar_url'  => $param[ 'avatarUrl' ],
            'gender'      => $param[ 'gender' ],
            'province'    => $param[ 'province' ],
            'city'        => $param[ 'city' ],
            'country'     => $param[ 'country' ],
            'update_time' => time(),
        ];
        if (empty($user)) {
            $data[ 'mobile' ]      = $param[ 'mobile' ];
            $data[ 'name' ]        = $param[ 'nickName' ];
            $data[ 'create_time' ] = time();
            $userId                = MallUser::insertGetId($data);
            $user                  = MallUser::where('id', $userId)->find();
        } else {
            if (empty($user[ 'mobile' ])) {
                $data[ 'mobile' ] = $param[ 'mobile' ];
            }
            if (empty($user[ 'name' ])) {
                $data[ 'name' ] = $param[ 'nickName' ];
            }
            if (false === MallUser::where('id', $user[ 'id' ])->update($data)) {
                _outPut(204, '', '修改失败');
            }
            $user = MallUser::where('id', $user[ 'id' ])->find();
        }
        
        _outPut(200, $user);
    }
}